package com.atguigu.dga.governance.assess.impl.cacl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assess.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("NO_ACCESS")
public class NoAccessAssessor extends Assessor {
    @Override
    protected void assess(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        //一张表{days}天内没有访问 则给0分 ， 其余给10
        //要提取 1.当天的考评日期  2.提取最后访问时间
        //计算 相减 和阈值对比 看是否大于阈值 如果大于给0分

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


        //1.提取当天的考评日期  //精确到天
        String todayDateStr = assessParam.getAssessDate();
        //将字符串日期先转为Date再转为时间戳
        Date todayDate = sdf.parse(todayDateStr);
        long todayTimeStamp = todayDate.getTime();


        //2.提取最后访问日期 //数据原始精确到秒 为了计算天数 精确到天
        Date lastSourceDate = assessParam.getTableMetaInfo().getTableLastAccessTime();
        String lastDateStr = sdf.format(lastSourceDate);
        //将字符串日期转为时间戳
        Date lastDate = sdf.parse(lastDateStr);
        long lastTimeStamp = lastDate.getTime();


        //3.将两个日期相减 和考评参数中的阈值对比
        //3.1取出阈值
        String jsonStr = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Long days = jsonObject.getLong("days");

        //3.2对两个日期相减获取到差值 //再计算得到精确到天的时间戳
        long diff = (todayTimeStamp - lastTimeStamp) / (1000 * 60 * 60 * 24);

        if (diff>days){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期未被访问:"+diff);
        }

    }
}
